package com.znbf.sfbx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

public class Signature {
    public static String getSign(Map<String,Object> map){
        ArrayList<String> list = new ArrayList<String>();
        map.put("serialVersionUID", "");
        for(Map.Entry<String,Object> entry:map.entrySet()){
            if(!entry.getValue().equals("") && entry.getValue()!= null){
                list.add(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        int size = list.size();
        String [] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        //Arrays.sort(arrayToSort, new CaseInsensitiveComparator());
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < size; i ++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result += "key=" + CallInterface.KEY;
        System.out.println("=Signature==="+"Sign Before MD5:" + result);
        result = MD5Util.encode(result).toUpperCase();
        System.out.println("=Signature==="+"Sign Result:" + result);
        return result;
    }
}
